package com.qire.manhua.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.common.basic.DateUtil;
import com.qire.common.constant.CacheStorageKey;
import com.qire.common.constant.DeviceConfigInfo;
import com.qire.common.constant.Navigation;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.BaseDialog;
import com.qire.common.utils.CacheStorageUtil;
import com.qire.manhua.R;
import com.qire.manhua.databinding.DialogMessageEnablePromptBinding;

import java.util.Date;

import androidx.annotation.NonNull;

public class MessageEnablePromptDialog extends BaseDialog {

    public static void showIfNeed(Context context) {
        long lastShowTime = CacheStorageUtil.queryLong(CacheStorageKey.MESSAGE_ENABLE_PROMPT_TIME, 0);
        boolean notNeedShow = lastShowTime < 0 || DeviceConfigInfo.messagePushEnable();
        boolean isLimitTime = DateUtil.yMdHms.assertToTodayValid(lastShowTime, 1);

        if(notNeedShow || isLimitTime || !UserEntity.self.isLogin()) {
            return;
        }

        MessageEnablePromptDialog dialog = new MessageEnablePromptDialog(context);
        dialog.show();
    }

    private final DialogMessageEnablePromptBinding viewDataBinder;

    private MessageEnablePromptDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog_Translucent);
        viewDataBinder = DialogMessageEnablePromptBinding.inflate(LayoutInflater.from(context));
        viewDataBinder.setMessageEnablePromptDialog(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(viewDataBinder.getRoot());
    }

    public void openBtnOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.MessageSetupPage).navigation();
        dismiss();
    }
    public void closeBtnOnClick(View view) {
        dismiss();
        CacheStorageUtil.save(CacheStorageKey.MESSAGE_ENABLE_PROMPT_TIME, new Date().getTime());
    }
    public void notPromptBtnOnClick(View view) {
        dismiss();
        CacheStorageUtil.save(CacheStorageKey.MESSAGE_ENABLE_PROMPT_TIME, -1);
    }
}
